package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.DishMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Override
    @Transactional //开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);  //mp会自动给id附一个雪花算法的值  因为在全局变量里设置了主键生成策略
        //菜品id
        Long dishid = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishid);
        }

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应口味信息
     *
     * @param id
     * @return
     */
    @Override
    public R<DishDto> getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);

        return R.success(dishDto);
    }

    /**
     * 修改菜品信息和对应口味信息
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish, "flavors");
        this.updateById(dish);
        //自己写的菜品口味更新方法但是只能添加.要实现删除更麻烦
//        List<DishFlavor> flavors = dishDto.getFlavors();
//        for(DishFlavor flavor:flavors){
//            if(flavor.getId()!=null){
//                dishFlavorService.updateById(flavor);
//            }else {
//                flavor.setDishId(dishDto.getId());
//                dishFlavorService.save(flavor);
//            }
//        }
        //老师思路先全部删除口味再添加提交过来的
        //全部删除
        Long dishId = dishDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(queryWrapper);
        //添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            dishFlavorService.save(flavor);
        }
    }

    /**
     * 批量删除 要求删除菜品表和菜品口味表 前提条件此菜品不在套餐中
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //获取菜品id判断是否在套餐明细表中
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getDishId, ids);
        //如果不在
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        if (list.size() == 0) {
            //删除对应的菜品口味表
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(DishFlavor::getDishId, ids);
            dishFlavorService.remove(lambdaQueryWrapper);
            //删除菜品表
            this.removeByIds(ids);
        } else {
            //如果在 不能删
            throw new CustomException("菜品还在套餐中不能删除");
        }
    }


}
